﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMTC.Dao.Role;
using SMTC.Model.Model;
using System.Data.Common;

namespace SMTC.BO.Role
{
    public class RoleProcessService
    {
        private RoleProcessDao _dao = new RoleProcessDao();

        public bool InsertRole(SMT_RoleModel role)
        {
            DbTransaction dbt = null;
            bool operResult = true;

            try
            {

                operResult = operResult && _dao.InsertRole(role, ref dbt);


                if (operResult == true)
                {
                    if (dbt != null) dbt.Commit();
                    return true;
                }
                else
                {
                    if (dbt != null) dbt.Rollback();
                    return false;
                }
            }
            catch
            {
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }
        public bool DeleteRole(string RoleID)
        {
            DbTransaction dbt = null;
            bool operResult = true;

            try
            {
                operResult = operResult && _dao.DeleteRole(RoleID, ref dbt);
                operResult = operResult && _dao.DELETE_MEMBER_IN_ROLE(RoleID,ref dbt);
                operResult = operResult && _dao.DELETE_ROLEFUNCTION(RoleID,ref dbt);

                if (operResult == true)
                {
                    if (dbt != null) dbt.Commit();
                    return true;
                }
                else
                {
                    if (dbt != null) dbt.Rollback();
                    return false;
                }
            }
            catch
            {
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }
        public bool INSERT_MEMBER_IN_ROLE(List<SMT_MemberInRole_Model> memberlist,string RoleID)
        {
            DbTransaction dbt = null;
            bool operResult = true;

            try
            {
                operResult = operResult && _dao.DELETE_MEMBER_IN_ROLE(RoleID,ref dbt);
                foreach (var member in memberlist)
                {
                    operResult = operResult && _dao.INSERT_MEMBER_IN_ROLE(member,ref dbt); ;
      
                }
                if (operResult == true)
                {
                    if (dbt != null) dbt.Commit();
                    return true;
                }
                else
                {
                    if (dbt != null) dbt.Rollback();
                    return false;
                }
            }
            catch
            {
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }
        public bool UPDATE_ROLE_FUCNTION(List<SMT_RoleFunctionModel> rolefunctionlist, string RoleID)
        {
            DbTransaction dbt = null;
            bool operResult = true;

            try
            {
                operResult = operResult && _dao.DELETE_ROLEFUNCTION(RoleID, ref dbt);
                foreach (var rolefunction in rolefunctionlist)
                {
                    operResult = operResult && _dao.INSERT_ROLEFUNCTION(rolefunction, ref dbt); ;

                }
                if (operResult == true)
                {
                    if (dbt != null) dbt.Commit();
                    return true;
                }
                else
                {
                    if (dbt != null) dbt.Rollback();
                    return false;
                }
            }
            catch
            {
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }
       
    }
}
